package com.ta.stage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ta.stage.entity.Dgroup;
import com.ta.stage.entity.vo.DgroupVo;
import com.ta.stage.result.Result;
import com.ta.stage.service.DgroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dgroup/")
@Api(tags={"设备分组 DgroupController"})
public class DgroupController {

    @Autowired
    private DgroupService dgroupService;

    @ApiOperation(value = "查询设备分组")
    @RequestMapping(value = "findDgroup",method = RequestMethod.GET)
    @ResponseBody
    public Result findDgroup(){
        List<Dgroup> dgroupList = dgroupService.list(null);
        return Result.ok(dgroupList);
    }

    @ApiOperation(value = "根据id查询设备分组")
    @GetMapping("getDgroup/{id}")
    public Result getDgroup(@PathVariable Long id){
         Dgroup dgroup = dgroupService.getById(id);
        return Result.ok(dgroup);
    }

    @ApiOperation(value = "新增设备分组")
    @RequestMapping(value = "saveDgroup",method = RequestMethod.POST)
    @ResponseBody
    public Result saveDgroup(@RequestBody Dgroup dgroup){
        boolean save = dgroupService.save(dgroup);
        return save?Result.ok():Result.fail();
    }

    @ApiOperation(value = "逻辑删除设备分组")
    @DeleteMapping("{id}")
    public Result removeDgroup(@PathVariable Long id){
        boolean flag = dgroupService.removeById(id);
        return flag?Result.ok():Result.fail();
    }

    @ApiOperation(value = "修改设备分组设置")
    @PostMapping("updateDgroup")
    public Result updateDgroup(@RequestBody Dgroup dgroup){
        boolean flag = dgroupService.updateById(dgroup);
        return flag?Result.ok():Result.fail();
    }

    @ApiOperation(value = "批量删除设备分组设置")
    @DeleteMapping("batchRemoveDgroup")
    public Result batchRemoveDgroup(@RequestBody List<Long> idList){
        dgroupService.removeByIds(idList);
        return Result.ok();
    }

    @ApiOperation(value = "设备分组查询带分页")
    @PostMapping("findPageDgroup/{current}/{limit}")
    public Result findPageDgroup(@PathVariable long current,
                               @PathVariable long limit,
                               @RequestBody(required = false) DgroupVo dgroupVo){

        //创建page对象，传递当前页，每页记录数
        Page<Dgroup> page = new Page<>(current,limit);

        //构建条件
        QueryWrapper<Dgroup> wrapper = new QueryWrapper<>();
        Integer id = dgroupVo.getId();
        String name = dgroupVo.getName();
        Integer policyId = dgroupVo.getPolicyId();
        //条件可能为空
        if (!StringUtils.isEmpty(id)){
            wrapper.eq("id",dgroupVo.getId());
        }
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",dgroupVo.getName());
        }
        if (!StringUtils.isEmpty(policyId)){
            wrapper.like("charge",dgroupVo.getPolicyId());
        }
        //调用方法实现分页查询
        Page<Dgroup> pageDgroup = dgroupService.page(page, wrapper);
        return Result.ok(pageDgroup);
    }
}

